package com.example.filecabinet.biz;

import com.example.filecabinet.dto.req.BizUserQueryReq;
import com.example.filecabinet.dto.resp.GeneralUserQueryResp;
import com.example.filecabinet.util.Http;
import com.thfdcsoft.framework.common.http.dto.BaseHttpRspn;
import com.thfdcsoft.framework.common.json.jackson.JacksonFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 链接外部接口
 *
 * @author wangy
 */
@Component
public class BizController {
    private static final Logger logger = LoggerFactory.getLogger(BizController.class);

    /**
     * 普通用户根据身份证去查询
     *
     * @author wangy
     */
    public static String getBusinessInfo(BizUserQueryReq g) {
        //String testJson ="{\"code\":\"1\",\"message\":\"查询成功\",\"data\":[{\"businessNumber\":\"213123123123\",\"businessType\":\"1\"},{\"businessNumber\":\"20200319128\",\"businessType\":\"2\"}]}";
        String rspn = null;
        String string = Http.sendGet("http://localhost:9090/business/query", "a");
        GeneralUserQueryResp generalUserQueryResp = JacksonFactory.readJson(string, GeneralUserQueryResp.class);
        logger.info("接口返回对象" + string);
        // 返回对象
        BaseHttpRspn result = new BaseHttpRspn(false);
        if (generalUserQueryResp.getCode() == "1" || generalUserQueryResp.getCode().equals("1")) {
            result.setResult(true);
            result.setRespJson(JacksonFactory.writeJson(generalUserQueryResp.getData()));
            result.setRespMsg("接口查询成功");
            return JacksonFactory.writeJson(result);
        } else {
            result.setResult(false);
            result.setRespJson(JacksonFactory.writeJson(generalUserQueryResp.getData()));
            result.setRespMsg("接口查询失败");
            return JacksonFactory.writeJson(result);
        }
    }

    //获取普通用户查询接口
    @Value("${generalUserQueryUrl}")
    private String generalUserQueryUrl;

    public static String generalUserUrl(@RequestBody String generalUserQueryReq) {
        //正式环境
//      String json2 = Http.sendPost(generalUserQueryUrl,generalUserQueryReq);
        //测试数据
        String testJson = RandomValue.businessNumber();
        GeneralUserQueryResp generalUserQueryResp = JacksonFactory.readJson(testJson, GeneralUserQueryResp.class);
        BaseHttpRspn result = new BaseHttpRspn(false);
        if (generalUserQueryResp.getCode() == "1" || generalUserQueryResp.getCode().equals("1")) {
            result.setResult(true);
            result.setRespObj(generalUserQueryResp.getData());
            result.setRespMsg("普通人查询待存业务接口查询成功");
            System.out.println(JacksonFactory.writeJson(result));
        } else {
            result.setResult(false);
            result.setRespObj(generalUserQueryResp.getData());
            result.setRespMsg("接口查询失败");
        }
        return JacksonFactory.writeJson(result);
    }
}
